/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernate.dao;

import hibernate.entity.ExamResult;
import hibernate.entity.Exams;
import java.util.List;

/**
 *
 * @author NDH
 */
public class ExamDAO extends AbstractDAO {

    /**
     * @return List<Exams>
     * @see A list table of Exams
     */
    public List<Exams> readAll() {
        query = getSession().createQuery("from Exams");
        return query.list();
    }

    /**
     * @return List<Exams>
     * @see A list table of Exams that is in the next time
     */
    public List<Exams> readLiving() {
        query = session.createQuery("from Exams where status = 'true' ");
        return query.list();
    }

    /**
     * @param Exams an string of exam's fields entered to create new one
     * @return int
     * @see Exams
     */
    public int create(Exams ex) {
        try {
            tx = session.beginTransaction();
            session.save(ex);
            tx.commit();
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * @param examID an ExamID to get a list of all exam results in the system
     * @return List<ExamResult>
     */
    public List<ExamResult> getByExam(int examID) {
        query = session.createQuery("from ExamResult e where e.exams.examId = :id");
        query.setParameter("id", examID);
        return query.list();
    }

    /**
     * @param Exams an Exam to remove from the system (set status into false)
     * @return int
     */
    public int remove(Exams ex) {
        try {
            tx = session.beginTransaction();
            ex.setStatus(false);
            ex = (Exams) session.merge(ex);
            session.update(ex);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
        }
        return 0;
    }

    /**
     * @param Exams an string of Exam's fields entered to update edited info
     * @return int
     * @see Exams
     */
    public int update(Exams ex) {
        try {
            tx = session.beginTransaction();
//            ex = (Exams) session.merge(ex);
            ex = (Exams) session.merge(ex);
            session.update(ex);
            tx.commit();
            return 1;
        } catch (Exception e) {
            tx.rollback();
        }
        return 0;
    }
}
